package cn.jinjimi.app.main.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;
import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.CardListBean;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;

/**
 * Created by Jinsen on 2015/5/14.
 */
public class CardListAdapter extends MyBaseAdapter<CardListBean.CardBean> {
    Context mContext = null;

    AbsListView.LayoutParams mIconLP = null;

    public CardListAdapter(Context context) {
        mContext = context;

        Resources res = mContext.getResources();
        int margin = res.getDimensionPixelSize(R.dimen.dp_10);
        int swidth = mContext.getResources().getDisplayMetrics().widthPixels;

        int singleIconWith = ((swidth - margin - margin - margin) / 2);
        int singleIconHeight = (int) (singleIconWith * 1.25);
        mIconLP = new AbsListView.LayoutParams(singleIconWith, singleIconHeight);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            holder = new Holder();
            SimpleDraweeView img = new SimpleDraweeView(mContext);
            GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(mContext.getResources())
                    .setPlaceholderImage(ContextCompat.getDrawable(mContext, R.drawable.logo), ScalingUtils.ScaleType.CENTER_CROP)
                    .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                    .build();
            img.setHierarchy(hierarchy);
            img.setLayoutParams(mIconLP);

            holder.img = img;
            convertView = img;
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        CardListBean.CardBean bean = getItem(position);
        holder.img.setImageURI(Uri.parse(bean.getCoverUrl()));

        return convertView;
    }

    class Holder {
        public ImageView img;
    }
}
